const cloud = getApp().cloud;
const app = getApp();

Page({
  data: {
    fixedObj:{
      w:218,    
      h:727,
      x:134,
      y:20
    },

    moveObj:[
      {
          w:196,    
          h:232,
          x:8,
          y:20,
          txt:'黑色',
          color:"#000",
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01PqjiCA20xZrW0H1Op_!!4161056916.png",
          isImgRef:''
      },
      {
          w:196,    
          h:232,
          x:8,
          y:140,
          txt:'粉色',
          color:"pink",
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01PqjiCA20xZrW0H1Op_!!4161056916.png",
          isImgRef:''
      },
      {
          w:196,    
          h:232,
          x:8,
          y:260,
          txt:'灰色',
          color:'#666',
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01PqjiCA20xZrW0H1Op_!!4161056916.png",
          isImgRef:''
      },
      {
          w:196,    
          h:232,
          x:266,
          y:20,
          txt:'绿色',
          color:'green',
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01PqjiCA20xZrW0H1Op_!!4161056916.png",
          isImgRef:''
      },
      {
          w:196,    
          h:232,
          x:266,
          y:140,
          txt:'红色',
          color:"red",
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01PqjiCA20xZrW0H1Op_!!4161056916.png",
          isImgRef:''
      },
      {
          w:196,    
          h:232,
          x:266,
          y:260,
          txt:'黄色',
          color:"yellow",
          disabled:false,
          opacity:1,
          imgRef:"https://img.alicdn.com/imgextra/i4/4161056916/O1CN01PqjiCA20xZrW0H1Op_!!4161056916.png",
          isImgRef:''
      }
   ],
   defaultImg:'https://img.alicdn.com/imgextra/i2/4161056916/O1CN01VsGya320xZrUEbqCk_!!4161056916.png',
   isDisabled:false,    //拖动块禁用
   num:7,    //选了哪个拖动块

  },
  movableChange(e){



    let kType = e.currentTarget.dataset.type    //移动的对象的下标
    let moveObjX = e.detail.x      //移动对象的x轴
    let moveObjY = e.detail.y      //移动对象的Y轴

    console.log(e);

    this.isConten(kType,moveObjX,moveObjY);

    //this.onTouchEnd(kType,moveObjX,moveObjY);



  },

  // 处理拖动的对象是否在区域内
  isConten(t,x,y){

    this.moveObj = this.data.moveObj

    console.log(this.data.moveObj[t],x,y);

    let color = this.data.moveObj[t].color

    if(x > this.data.fixedObj.x - (this.data.moveObj[t].w * this.data.bili) / 2 && x < (this.data.fixedObj.x + this.data.bili * this.data.fixedObj.w - (this.data.moveObj[t].w * this.data.bili) / 2)){

      console.log("到了",t);

      this.setData({

        [`moveObj[${t}].opacity`]:0,

        [`moveObj[${t}].x`]:this.data.moveObj[t].x+0.01,

        defaultImg: this.data.moveObj[t].isImgRef,

        isDisabled: true,

        num: t,

        id: this.data.moveObj[t].id
      })

      console.log("xxxxxxxx===========>",t)


    }else{

      //回到原位
      this.setData({
        [`moveObj[${t}].x`]:this.moveObj[t].x
      })
      //console.log(this.data.moveObj[t].x)

    }

  },


  //重置
  cz(){

     for(let i in this.data.moveObj){

        this.setData({
          [`moveObj[${i}].disabled`]: false,
          [`moveObj[${i}].opacity`]: 1
        })

      }

      this.setData({
        color:'',
        isDisabled:false,
        num:7,
        defaultImg:this.data.kwIdImg
      })

  },


  //选好了
  yes(){

    //app.globalData.innerAudioContext.pause();

     //my.alert({content:`${JSON.stringify(app.globalData.innerAudioContext)}`})

    if(this.data.num == 7){

      my.showToast({
        content:"您还未选择文案"
      })
      
    }else{

      this.selectWA();
      
    }

  },

   //获取文案列表
  getWAList(){


    cloud.function.invoke('client',{},'getDeclarationList').then(res => {
      console.log('game2---文案列表',res);  

      //itemLogo == isImgRef
      //mainLogo == imgRef

      for(let i in res.data){

        this.setData({
          [`moveObj[${i}].imgRef`]: res.data[i].itemLogo,
          [`moveObj[${i}].isImgRef`]: res.data[i].mainLogo,
          [`moveObj[${i}].id`]: res.data[i]._id
        })
        
      } 

      
    }) 


   
  },

  selectWA(){

    console.log(this.data.id,this.data.kwId);

    //return

    cloud.function.invoke('client',{_id:this.data.kwId},'pickFlavor').then(res => {

      console.log('game2---选择口味瓶身',res);  

      if(res.code == 0){
        
        cloud.function.invoke('client',{_id:this.data.id},'pickDeclaration').then(res => {

          console.log('game2---选择文案',res);  

          if(res.code == 0){

            //保存口味和宣言的组合图   
            cloud.function.invoke('client',{combinationImg:this.data.defaultImg},'setCombinationImg').then(res => {
              
              console.log('game2---保存口味和宣言的组合图',res);  
              
              if(res.code == 0){
              
                my.redirectTo({ url: `../share/share?bs=${1}&defaultImg=${this.data.defaultImg}` })

              }
            })

            
          }else{
            my.showToast({
              content:res.msg
            })
          }

          //itemLogo == isImgRef
          //mainLogo == imgRef

          // for(let i in res.data){

          //   this.setData({
          //     [`moveObj[${i}].imgRef`]: res.data[i].mainLogo,
          //     [`moveObj[${i}].isImgRef`]: res.data[i].itemLogo,
          //   })
            
          // } 
          
        }) 

      }else{
        my.showToast({
          content:res.msg
        })
      } 
    }) 
  },

  
  onLoad(query) {

    console.log(query.id,query.defaultImg);

    this.setData({
      kwId:query.id,
      kwIdImg:query.defaultImg,
      defaultImg:query.defaultImg
    })

    this.setData({
      bili: my.getSystemInfoSync().windowWidth / 750
    })

    this.getWAList()

  },

  onShow(){

  }

});
